//
//  AudioTool.m
//  HaoHint
//
//  Created by winter on 16/5/26.
//  Copyright © 2016年 wd. All rights reserved.
//

#import "AudioTool.h"
#import <AudioToolbox/AudioToolbox.h>

@implementation AudioTool

static SystemSoundID   soundFileObject;

+ (void)stopAudio
{
    if (soundFileObject) {
        [self stopAlertSoundWithSoundID:soundFileObject];
    }
}

+ (void)playAudio:(NSString *)sound
{
    if (soundFileObject) {
        [self stopAlertSoundWithSoundID:soundFileObject];
    }
    
    NSString *strSoundFile = [[NSBundle mainBundle] pathForResource:sound ofType:@"caf"];
    CFURLRef soundFileURLRef = (__bridge CFURLRef)[NSURL fileURLWithPath:strSoundFile];
    
    AudioServicesCreateSystemSoundID(soundFileURLRef,&soundFileObject);
    AudioServicesPlaySystemSound(soundFileObject);
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

+ (void)stopAlertSoundWithSoundID:(SystemSoundID)sound
{
    AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);
    AudioServicesDisposeSystemSoundID(sound);
    AudioServicesRemoveSystemSoundCompletion(sound);
}
@end
